﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// ZhValueComm 的摘要说明：珠华平台控阀请求处理类
    /// </summary>
    public class ZhValueComm
    {
        public static string str_result = "";
        public static string str_jsonParam = "";
        public static string str_zxjg = "1";                   //执行结果  -  1：已调用

        public static string result(string MeterId, string Type, string CommPam)
        {
            string url = System.Configuration.ConfigurationManager.AppSettings["ZhValueComm_URL"].ToString();

            //NB控阀指令：
            //十进制：67
            if (Type == "67")
            {
                string str_value = "";

                if (CommPam == "85")
                {
                    str_value = "1";
                }
                else if (CommPam == "153")
                {
                    str_value = "0";
                }
                else
                {
                    str_result = "未开放指令";
                    return str_result;
                }
                
                string str_timestamp = WebSdkCore.GetTimeStamp();

                string str_data = "";
                str_data = "meter_number=[\"" + MeterId + "\"]&valve_status=" + str_value + "&timestamp=" + str_timestamp + "&secret=8e20f76567";

                string str_sign = "";
                str_sign = WebSdkCore.GetMD5String(str_data);

                str_jsonParam = "{\"meter_number\":[\"" + MeterId + "\"],\"valve_status\":" + str_value + ",\"timestamp\":\"" + str_timestamp + "\",\"sign\":\"" + str_sign + "\"}";

                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                byte[] byteData = Encoding.UTF8.GetBytes(str_jsonParam);

                int length = byteData.Length;
                request.ContentLength = length;
                Stream writer = request.GetRequestStream();
                writer.Write(byteData, 0, length);
                writer.Close();

                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();

                str_result = responseString.ToString();

                str_result = NB_sql(MeterId, Type);
            }
            else
            {
                str_result = "未开放指令";
            }
            return str_result;
        }

        public static string NB_sql(string MeterId, string Type)
        {
            DateTime str_dt = DateTime.Now;

            bool operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_cx(ybdz,mllx,mlnr,zxsj,zxjg,fhnr) values ('" +
                                                                MeterId + "','" +
                                                                Type + "','" +
                                                                str_jsonParam + "','" +
                                                                str_dt + "','" +
                                                                str_zxjg + "','" +
                                                                str_result + "')");

            if (operatorlog == true)
            {
                str_result = "成功";
            }
            else
            {
                str_result = "写指令失败";
            }

            return str_result;
        }
    }
}